﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;

namespace RiskMan.DataClass.Models.Plaza_II.FortsFutInfoRepl
{
    /// <summary>
    /// Таблица 28. Поля таблицы fut_rejected_orders - Отвергнутые в клиринг заявки
    /// </summary>
    public class FutRejectedOrders: INotifyPropertyChanged
    {
        #region private

        private long _replId;
        private long _replRev;
        private long _replAct;
        private long _orderId;
        private int _sessId;
        private string _clientCode;
        private DateTime _moment;
        private DateTime _momentReject;
        private int _isinId;
        private int _dir;
        private int _amount;
        private Decimal _price;
        private DateTime _dateExp;
        private long _idOrd1;
        private int _retCode;
        private string _retMessage;
        private string _comment;
        private string _loginFrom;
        private int _extId;

        #endregion

        #region public

        /// <summary>
        /// Служебное поле подсистемы репликации
        /// </summary>
        public long ReplId
        {
            get { return _replId; }
            set
            {
                if (_replId != value)
                {
                    _replId = value;
                    RaisePropertyChanged("ReplId");
                }
            }
        }

        /// <summary>
        /// Служебное поле подсистемы репликации
        /// </summary>
        public long ReplRev
        {
            get { return _replRev; }
            set
            {
                if (_replRev != value)
                {
                    _replRev = value;
                    RaisePropertyChanged("ReplRev");
                }
            }
        }

        /// <summary>
        /// Служебное поле подсистемы репликации
        /// </summary>
        public long ReplAct
        {
            get { return _replAct; }
            set
            {
                if (_replAct != value)
                {
                    _replAct = value;
                    RaisePropertyChanged("ReplAct");
                }
            }
        }

        
        /// <summary>
        /// Номер заявки
        /// </summary>
        public long OrderId
        {
            get { return _orderId; }
            set
            {
                if (_orderId != value)
                {
                    _orderId = value;
                    RaisePropertyChanged("OrderId");
                }
            }
        }
        
        /// <summary>
        /// Идентификатор торговой сессии
        /// </summary>
        public int SessId
        {
            get { return _sessId; }
            set
            {
                if (_sessId != value)
                {
                    _sessId = value;
                    RaisePropertyChanged("SessId");
                }
            }
        }
        
        /// <summary>
        /// Код клиента
        /// </summary>
        public string ClientCode
        {
            get { return _clientCode; }
            set
            {
                if (_clientCode != value)
                {
                    _clientCode = value;
                    RaisePropertyChanged("ClientCode");
                }
            }
        }
        
        /// <summary>
        /// Время изменения состояния заявки
        /// </summary>
        public DateTime Moment
        {
            get { return _moment; }
            set
            {
                if (_moment != value)
                {
                    _moment = value;
                    RaisePropertyChanged("Moment");
                }
            }
        }
        
        /// <summary>
        /// Время, когда заявка была отвергнута
        /// </summary>
        public DateTime MomentReject
        {
            get { return _momentReject; }
            set
            {
                if (_momentReject != value)
                {
                    _momentReject = value;
                    RaisePropertyChanged("MomentReject");
                }
            }
        }

        /// <summary>
        /// Уникальный числовой идентификатор инструмента
        /// </summary>
        public int IsinId
        {
            get { return _isinId; }
            set
            {
                if (_isinId != value)
                {
                    _isinId = value;
                    RaisePropertyChanged("IsinId");
                }
            }
        }
        
        /// <summary>
        /// Направление
        /// </summary>
        public int Dir
        {
            get { return _dir; }
            set
            {
                if (_dir != value)
                {
                    _dir = value;
                    RaisePropertyChanged("Dir");
                }
            }
        }
        
        /// <summary>
        /// Объём, количество единиц инструмента
        /// </summary>
        public int Amount
        {
            get { return _amount; }
            set
            {
                if (_amount != value)
                {
                    _amount = value;
                    RaisePropertyChanged("Amount");
                }
            }
        }
        
        /// <summary>
        /// Цена
        /// </summary>
        public Decimal Price
        {
            get { return _price; }
            set
            {
                if (_price != value)
                {
                    _price = value;
                    RaisePropertyChanged("Price");
                }
            }
        }
        
        /// <summary>
        /// Дата истечения заявки
        /// </summary>
        public DateTime DateExp
        {
            get { return _dateExp; }
            set
            {
                if (_dateExp != value)
                {
                    _dateExp = value;
                    RaisePropertyChanged("DateExp");
                }
            }
        }
        /// <summary>
        /// Номер первой заявки
        /// </summary>
        public long IdOrd1
        {
            get { return _idOrd1; }
            set
            {
                if (_idOrd1 != value)
                {
                    _idOrd1 = value;
                    RaisePropertyChanged("IdOrd1");
                }
            }
        }
        
        /// <summary>
        /// Код возврата процедуры перепостановки
        /// </summary>
        public int RetCode
        {
            get { return _retCode; }
            set
            {
                if (_retCode != value)
                {
                    _retCode = value;
                    RaisePropertyChanged("RetCode");
                }
            }
        }
           
        /// <summary>
        /// Текст сообщения о причине отвержения заявки при перепостановке
        /// </summary>
        public string RetMessage
        {
            get { return _retMessage; }
            set
            {
                if (_retMessage != value)
                {
                    _retMessage = value;
                    RaisePropertyChanged("RetMessage");
                }
            }
        }
        
        /// <summary>
        /// Комментарий трейдера
        /// </summary>
        public string Comment
        {
            get { return _comment; }
            set
            {
                if (_comment != value)
                {
                    _comment = value;
                    RaisePropertyChanged("Comment");
                }
            }
        }
        
        /// <summary>
        /// Логин пользователя, поставившего заявку
        /// </summary>
        public string LoginFrom
        {
            get { return _loginFrom; }
            set
            {
                if (_loginFrom != value)
                {
                    _loginFrom = value;
                    RaisePropertyChanged("LoginFrom");
                }
            }
        }
        
        /// <summary>
        /// Внешний номер
        /// </summary>
        public int ExtId
        {
            get { return _extId; }
            set
            {
                if (_extId != value)
                {
                    _extId = value;
                    RaisePropertyChanged("ExtId");
                }
            }
        }

        #endregion

        #region event PropertyChangedEventHandler

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region processing
        public static FutRejectedOrders ParseFutInstruments(Dictionary<String, String> relation)
        {
            FutRejectedOrders futRejectedOrders = new FutRejectedOrders();
            futRejectedOrders.Parse(relation);
            return futRejectedOrders;
        }

        public void Parse(Dictionary<String, String> relation)
        {
            foreach (String field in relation.Keys)
            {
                String value = relation[field];

                switch (field)
                {
                    case "replID": long.TryParse(value, out _replId); break;
                    case "replRev": long.TryParse(value, out _replRev); break;
                    case "replAct": long.TryParse(value, out _replAct); break;
                    case "order_id": long.TryParse(value, out _orderId); break;
                    case "sess_id": int.TryParse(value, out _sessId); break;
                    case "client_code": _clientCode = value; break;
                    case "moment": DateTime.TryParse(value, out _moment); break;
                    case "moment_reject": DateTime.TryParse(value, out _momentReject); break;
                    case "isin_id": int.TryParse(value, out _isinId); break;
                    case "dir": int.TryParse(value, out _dir); break;
                    case "amount": int.TryParse(value, out _amount); break;
                    case "price": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _price); break;
                    case "date_exp": DateTime.TryParse(value, out _dateExp); break;
                    case "id_ord1": long.TryParse(value, out _idOrd1); break;
                    case "ret_code": int.TryParse(value, out _retCode); break;
                    case "ret_message": _retMessage = value; break;
                    case "comment": _comment = value; break;
                    case "login_from": _loginFrom = value; break;
                    case "ext_id": int.TryParse(value, out _extId); break; 

                };
            }
        }
        #endregion


        public static void AddValueToObsColFutRejectedOrders(FutRejectedOrders inst)
        {
            MainWindow.StaticLevel._dataProvider.FutRejectedOrdersList.Add(inst);
        }
    }
}
